<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../statics/css/login.css">
</head>
<body>
<div id="wrapper" class="login-page">
    <div id="" class="form">
        <form class="login-form">
            <h2>管理登录</h2>
            <div>
                <input type="text" placeholder="学号" id="userNo" name="userNo" value=""/>
            </div>
            <div>
                <input type="password" placeholder="密码" id="password" name="password" value=""/>
            </div>
            <div>
                <input type="text" placeholder="验证码" id="verify_code" name="verificationCode" style="width: 70%;float: left" value=""/>
                <div style="float: right;width: 30%;height: 100%"><img style="color: #55b7a4;" src="/verificationCode"
                                                                      onclick="updateCode(this)" class="login_txtbx"
                                                                      id="updataCodeId">
                </div>
            </div>


            <div>
                 <input id="loginId" type="button" onclick="check_login()" value="登录" style="background: #0dc316"/>
            </div>
            <p class="message" >还没有账户? <a onclick="register()">立刻创建</a></p>
        </form>
    </div>
</div>
<script src="../../statics/lib/layui/layui.js" charset="utf-8"></script>
<script src="../../statics/js/jquery.min.js"></script>
<script>
    var layer;
    layui.use('layer', function () {
       layer = layui.layer;
    });
    function check_login() {
        var registerInfo = $(".login-form").serialize();
        registerInfo= decodeURIComponent(registerInfo,true);
        var json =strToJson(registerInfo);
        $.ajax({
            type:"POST",
            url:"/user/doLogin",
            data:{
                username:$("#userNo").val(),
                password:$("#password").val(),
                verificationCode:$("#verify_code").val()},
            success:function (result) {
                if(0==result.code){
                    debugger;
                    layer.msg(result.msg,{icon: 1, time: 1000},function () {
                        window.location.href = "/doIndex";
                    });
                }else{
                    layer.msg(result.msg,{icon: 5, time: 1000});
                }
            },
            error:function () {
                layer.msg("登录失败",{icon: 5, time: 1000});
            }
        })
    }

    function register() {
        layer.msg("试运行阶段不可注册，请联系管理员",{icon: 5, time: 1000});
        //window.location.href = "/doRegister";
    }

    function strToJson(data){
        data=data.replace(/&/g,"\",\"");
        data=data.replace(/=/g,"\":\"");
        data="({\""+data+"\"})";
        var obj = eval(data);
        return obj;
    }

    function updateCode(e) {
        var source = e.src;
        var index = source.indexOf("?");
        if (index > -1) {
            var s = source.substring(0, index);
            var date = new Date();
            var time = date.getTime();
            e.src = s + "?time=" + time;
        } else {
            var date = new Date();
            e.src = source + "?time=" + date.getTime();
        }
    }
    $("body").keydown(function () {
        if (event.keyCode == "13"){    //13是enter键的键码
            $("#loginId").click();
            //调用登录方法,在div中定义方法,或通过js绑定的方法都可以,我的登录方法就是通过jquery绑定的点击事件
        }
    });
</script>
</body>
</html>